/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bayu.services.dao;

import com.bayu.services.entity.Stock;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author submonkey
 */
@Repository("ssDao")
@Transactional
public class StockDao  {

    private HibernateTemplate hibernateTemplate;

    @Autowired
    public void setSesscionFactory(SessionFactory sessionFactory) {
        hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Transactional(readOnly=false)
    public void save(Stock stock) {
        hibernateTemplate.saveOrUpdate(stock);
    }

    @Transactional(readOnly=false)
    public void delete(Stock stock) {
        hibernateTemplate.delete(stock);
    }

    @Transactional(readOnly=false)
    public void update(Stock stock) {
        hibernateTemplate.update(stock);
    }

    public List<Stock> getAll(String query) {
        return hibernateTemplate.find(query);
    }

    public List<Stock> getAllByDate(String start, String end) {
        return hibernateTemplate.find("from Stock where date between '"
            + start + "' and '" + end + "' order by date asc");
    }

    public List<Stock> getById(Long id) {
        return hibernateTemplate.find("from Stock where id = " + id);
    }

    public List<Stock> getAllByMonth(String month) {
        return hibernateTemplate.find("from Stock where MONTH(date)='" + month + "' order by date asc");
    }

    public List custom(String query) {
        return hibernateTemplate.find(query);
    }
}
